﻿using CQRS.Events;
using CQRS.Sample.Domain.BOs;
using CQRS.Sample.Domain.Commands;

namespace CQRS.Sample.Domain.CommandHandlers
{
    public class RegisterPlaneCrashCommandHandler : ICommandHandler<RegisterPlaneCrashCommand>
    {
        private readonly IDomainRepository<Event> _repository;

        public RegisterPlaneCrashCommandHandler(IDomainRepository<Event> repository)
        {
            _repository = repository;
        }

        #region ICommandHandler<RegisterPlaneCrashCommand> Members

        public void Execute(RegisterPlaneCrashCommand theEvent)
        {
            var crashedPlane = _repository.GetById<Plane>(theEvent.PlaneId);
            crashedPlane.PlaneCrashed(theEvent.PlaneId, theEvent.Location, theEvent.Route,
                theEvent.Fatalities, theEvent.Aboard, theEvent.CrashDate);
            _repository.Add(crashedPlane);
            _repository.Commit();
        }

        #endregion
    }
}